<?php

class BizController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
        
        $data = new Application_Model_Busines;
        //$id = Zend_Auth::getInstance()->getIdentity();
        //$data->getBiz(1);
       /// тут будет получение айди юзера
        $this->view->data = $data->getBiz(1);
    }
    
    public function successAction()
    {
        //Надпись об удачном добавлении
    }
    
    public function addAction()
    {
        $form = new Application_Form_BizAdd();
        
        if($this->getRequest()->isPost()){
            if($form->isValid($this->getRequest()->getPost()))
            {
                $biz = new Application_Model_Busines();
                $biz->fill($form->getValues());
                $biz->created = date('Y-m-d H:i:s');
                $biz->user_id = 1; // тут мы добавлять айди из сесси
                $biz->activated = 1; // тут заглушка потом модератор
                //$biz->type_id;
                $biz->save();
                $model = new Application_Model_Search();
                $model->updateIndex();
                $this->_helper->redirector('index');
            }
        }
        $this->view->form = $form;
    }
    
    public function editAction()
    {
        
        $id = $this->_getParam('id');
        $form = new Application_Form_BizAdd();
        $biz = new Application_Model_Busines($id);
        if($biz->user_id == 1){  /// тут будет из сессии
        
        if($this->getRequest()->isPost()){
            if($form->isValid($this->getRequest()->getPost()))
            {
                $biz->fill($form->getValues());
                $biz->modified = date('Y-m-d H:i:s');
                //$biz->type_id;
                
                $biz->save();
                $model = new Application_Model_Search();
                $model->updateIndex();
                $this->_helper->redirector('index');
            } else {
                $form->populate($biz->populateForm());
            }
        }
        
        $form->populate($biz->populateForm());
        $this->view->form = $form;
        
      } else {
          $this->_helper->redirector('404');    
      }
      
    }
    public function viewAction()
    {
        $id = $this->_getParam('id');
        $biz = new Application_Model_Busines($id);
        if($biz->user_id == 1){  /// тут будет из сессии
            $classes = new Application_Model_Class();
            $this->view->biz = $biz;
            $this->view->classes = $classes->getClasses($id);
        } else {
          throw new Zend_Controller_Action_Exception('Your message here', 404); //  
      }
    }
    
    public function claddAction()
    {
        $form = new Application_Form_ClassAdd();
        $id = $this->_getParam('id');
        $biz = new Application_Model_Busines($id);
        if($biz->user_id == 1){
        
            if($this->getRequest()->isPost()){
            if($form->isValid($this->getRequest()->getPost()))
            {
                $class = new Application_Model_Class();
                $class->fill($form->getValues());
                $class->business_id = $id;
                $class->save();
                $this->_helper->redirector('index');
            }
        }
        $this->view->form = $form;
        } else {
          $this->_helper->redirector('404');    
      }
    }
    
     public function cleditAction()
     {
        
        $id = $this->_getParam('id');
        $form = new Application_Form_ClassAdd();
        $class = new Application_Model_Class($id);
        $biz = new Application_Model_Busines($class->business_id);
        if($biz->user_id == 1){  /// тут будет из сессии
        
        if($this->getRequest()->isPost()){
            if($form->isValid($this->getRequest()->getPost()))
            {
                $class->fill($form->getValues());
                //$biz->type_id;
                
                $class->save();
                $this->_helper->redirector('index');
            } else {
                $form->populate($biz->populateForm());
            }
        }
        
        $form->populate($class->populateForm());
        $this->view->form = $form;
        
      } else {
          $this->_helper->redirector('404');    
      }
      
    }
    public function cldeleteAction()
    {
        $id = $this->_getParam('id');
        $class = new Application_Model_Class($id);
        $biz = new Application_Model_Busines($class->business_id);
        if($biz->user_id == 1){  /// тут будет из сессии
        $class->delete();
        $this->_helper->redirector('index');
        } else {
            $this->_helper->redirector('404');  
        }
    
    }
    public function deleteAction()
    {
        $id = $this->_getParam('id');
        $biz = new Application_Model_Busines($id);
        if($biz->user_id == 1){  /// тут будет из сессии
        $biz->delete();
        $model = new Application_Model_Search();
        $model->updateIndex();
        $this->_helper->redirector('index');
        } else {
            $this->_helper->redirector('404');  
        }
    
    }
}
